<?php
class PrivTableHelper {
    /**
     * 取树形结构
     */
    static function getTree($pid=0, $inc_hidden=0) {
        $list = self::_getList();
		if(!isset($list[$pid])){ $list[$pid]=array(); }

		try{
			if($inc_hidden) {
				foreach($list as &$p) {
					$list[$p['pid']]['nodes'][$p['id']]     =&   $p;
				}
				unset($p);
			}
			else {
				foreach($list as &$p) {
					if(0< $p['is_show']) {
						$list[$p['pid']]['nodes'][$p['id']]     =&   $p;
					}
					unset($p);
				}
			}
		} catch(Exception $e){}

        return $list[$pid]['nodes'];
    }

    /**
     * 返回用户可见的菜单树形结构
     * @param array $pa priv_id的数组
     */
    static function getTreeForMenu($pa)
    {
        if(!is_array($pa) || !$pa)
            return array();

        $list	= self::_getList();
        foreach($list as $k=>$p) {
            if($p['is_show']==0 || !in_array($p['id'], $pa)) {
                unset($list[$k]);
            }
        }
        $list[0]    =   array();
        foreach($list as &$p) {
            if(isset($list[$p['pid']]))
                $list[$p['pid']]['nodes'][$p['id']] =&   $p;
			unset($p);
        }

        return $list[0]['nodes'];
    }

    static function getByCode($code)
    {
        $_list  =   self::_getList();
        foreach($_list as $f) {
            if(0===strcmp($f['code'],$code))
                return $f;
        }
        return false;
    }


    /*-------------------------------protected method--------------------------*/

    protected static function _getList()
    {
        static $_list = null;
        if(!$_list) {
            $_list  = PrivTable::getList();
        }
        return $_list;
    }

}
?>
